<template>
    <div>
        <!-- 导航栏 -->
        <van-nav-bar left-arrow @click-left="onClickLeft">
            <template #title>
                <span>我的收货地址</span>
            </template>
        </van-nav-bar>
        <!-- 地址列表 -->
        <van-address-list
            v-model="chosenAddressId"
            :list="list"
            
            :disabled-list="disabledList"
            disabled-text="以下地址超出配送范围"
            default-tag-text="默认"
            @add="onAdd"
            @edit="onEdit"
            @click-item="clickItem"
        />
    </div>
</template>

<script>
import { Toast } from 'vant';
import axios from "axios";
export default {
    name:"MyAddress",
    data() {
        return {
            chosenAddressId: '1',
            list: [],
            disabledList: [
                {
                    id: '3',
                    name: '王五',
                    tel: '1320000000',
                    address: '浙江省杭州市滨江区江南大道 15 号',
                },
            ],
            token:'Bearer ' + localStorage.getItem("token"),
        }
    },
    methods:{
        onClickLeft(){
            // 后退一步
          this.$router.go(-1);
        },
        clickItem(item){
            this.$store.state.address = item;
            this.$router.go(-1);
            console.log(item);
        },
        onAdd() {
            this.$router.push({path:"/addressEdit"});
        },
        onEdit(item, index) {
            console.log(item);
            this.$router.push({path:"/UpdateAddressEdit/"+item.id});
        },
        queryAddress(){
            let that = this
            console.log(this);
            const instance = axios.create({
                baseURL: 'https://api.example.com',
                timeout: 5000,
                headers: {
                    // 设置请求头，把 token 传递给服务器程序，以便于认证
                    'Authorization': that.token
                }
            });
            // 发起请求
            instance.get('http://localhost:9090/api/user/Address',{
                params:{
                    uid:this.$store.state.uid,
                }
            })
            .then(response => {
                // 处理响应数据
                console.log(response.data);
                if (response.data.code === 200) {
                    this.list = response.data.data;
                    this.list.forEach(address => {
                        this.$set(address, 'address', address.addressDetail);
                    })
                } else{
                    this.$store.state.isLogin = false
                }
            })
            .catch(error => {
                // 处理错误
                console.error(error);
            });
        }
    },
    mounted() {
         this.queryAddress()
    }
}
</script>

<style>

</style>